package com.wwy.arithmetic.suanfa;

import com.wwy.arithmetic.suanfa.entry.TreeNode;

/**
 * 判断两个树是否相等
 */
public class JudgeTree {


    public static void main(String[] args) {
        TreeNode treeNodeA = new TreeNode();
        treeNodeA.setVal(1);
        TreeNode treeNodeA2 = new TreeNode();
        treeNodeA2.setVal(2);
        TreeNode treeNodeB = new TreeNode();
        treeNodeB.setVal(1);
        TreeNode treeNodeB1 = new TreeNode();
        treeNodeB1.setVal(2);
        treeNodeA.setLeft(treeNodeA2);
        treeNodeB.setRight(treeNodeB1);
        boolean sameTree = isSameTree(treeNodeA, treeNodeB);
        System.out.println("sameTree = " + sameTree);
    }

    /**
     * 1       1
     * 2               2
     */
    public static boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p == null || q == null) {
            return false;
        } else if (p.getVal() != q.getVal()) {
            return false;
        }else{
            return isSameTree(p.getLeft(),q.getLeft())&& isSameTree(q.getRight(),p.getRight());
        }
    }


}
